package com.jumi.microservice.stock.controller;

import com.jumi.common.log.annotation.Log;
import com.jumi.common.log.enums.BusinessType;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.stock.domain.CommodityRenderVo;
import com.jumi.microservice.stock.dto.StockMaterialStockDTO;
import com.jumi.microservice.stock.dto.commodity.CommodityApplyDTO;
import com.jumi.microservice.stock.service.CommodityApplyService;
import com.jumi.microservice.stock.service.StockService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author ssk
 * @create 2020/9/1 9:54
 **/
@RestController
@RequestMapping("/stock/commodity")
@Api(tags = {"【仓库管理】商城商品仓库分配"})
//@DubboService
public class StockCommodityController extends BaseController {

    @Resource
    private CommodityApplyService applyService;

    @Resource
    private StockService stockService;




    @PostMapping("/addCommodityApply")
    @Log(title = "添加分仓申请", businessType = BusinessType.INSERT)
    public ResponseResult addCommodityApply(@RequestBody CommodityApplyDTO applyRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String createBy) {

        applyRequest.setCreateBy(createBy);
        int rows = applyService.save(applyRequest);
        return ResponseResult.success("操作成功!", applyRequest.getId());
    }

    @GetMapping("/findByPage")
    @ApiOperation("分页查询分仓申请列表")
    public TableDataInfo<CommodityApplyDTO> findByPage(CommodityApplyDTO applyRequest) {
        startPage();
        List<CommodityApplyDTO> list = applyService.findByPage(applyRequest);
        return getDataTable(list);
    }

    @GetMapping("/findById")
    @ApiOperation("根据仓库id查询分仓申请详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "仓库分仓申请ID"),
            @ApiImplicitParam(name = "type", value = "分仓类型;0=一般分仓,1=补货分仓")
    })
    public ResponseResult<CommodityApplyDTO> findById(Long id, Integer type) {
        CommodityApplyDTO applyResponse = applyService.findById(id, type);
        return ResponseResult.success(applyResponse);
    }


    @GetMapping("/findFreeStock")
    @ApiOperation("根据仓库id 物料id集合获取物料可用库存")
    public ResponseResult findFreeStock(Long warehouseId, Long[] materialIds) {
        List<Long> materialIdList = Arrays.asList(materialIds);
        List<StockMaterialStockDTO> list = stockService.getMaterialStock(materialIdList, warehouseId);
        return ResponseResult.success(list);
    }

    @PostMapping("/renderStock")
    @ApiOperation("分配库存")
    @Log(title = "供应链管理-仓库管理-仓库分仓", businessType = BusinessType.INSERT)
    public ResponseResult renderStock(@RequestBody CommodityRenderVo renderVo, @RequestHeader(RequestConstant.ADMIN_NAME) String createBy) {
        int rows = applyService.render(renderVo, createBy);
        return ResponseResult.success("操作成功!", renderVo.getApplyId());
    }


}
